#include "att.h"

union ATT_VALUE ATT;

void ATT_Init(void)
{   

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    GPIO_InitConfig(GPIOB, GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15, GPIO_Mode_Out_PP, GPIO_Speed_50MHz);
    GPIO_WriteBit(GPIOB, GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15, Bit_RESET);
}
//按照真值表输入值
void ATT_PinSet(uint8_t value)
{
    uint16_t new_io = (uint16_t)value << 10;
    uint16_t now_io = GPIO_ReadOutputData(GPIOB) & 0x03ff;  //读取当前GPIOB的输出状态，再将PB10-PB15的数据位置零
    GPIO_Write(GPIOB, new_io|now_io);
}
//输入衰减值，按照HMC472A的真值表输出近似的衰减值，并返回设置的值
uint16_t ATT_RatioSet(float value)
{
    if(value < 0)
    {
        GPIO_WriteBit(GPIOB, GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15, Bit_SET);
        return 1;
    }
    else if(value > 31.5)
    {
        GPIO_WriteBit(GPIOB, GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15, Bit_RESET);
        return 0;
    }
    else
    {
        uint16_t val = (int)(value * 10);
        uint16_t new_io = ALL_HIGH;
        uint16_t now_io = GPIO_ReadOutputData(GPIOB) & 0x03ff;  //读取当前GPIOB的输出状态，再将PB10-PB15的数据位置零
        
        //根据val的值，计算出new_io的值
        if(val/160)
        {
            new_io &= V1_16_dB;
            val -= 160;
            if(val/80)
            {
                new_io &= V2_8_0dB;
                val -= 80;
                if(val/40)
                {
                    new_io &= V3_4_0dB;
                    val -= 40;
                    if(val/20)
                    {
                        new_io &= V4_2_0dB;
                        val -= 20;
                        if(val/10)
                        {
                            new_io &= V5_1_0dB;
                            val -= 10;
                            if(val/5)
                            {
                                new_io &= V6_0_5dB;
                                val -= 5;
                            }
                        }
                    }
                }
            }
        }
        else if(val/80)
        {
            new_io &= V2_8_0dB;
            val -= 80;
            if(val/40)
            {
                new_io &= V3_4_0dB;
                val -= 40;
                if(val/20)
                {
                    new_io &= V4_2_0dB;
                    val -= 20;
                    if(val/10)
                    {
                        new_io &= V5_1_0dB;
                        val -= 10;
                        if(val/5)
                        {
                            new_io &= V6_0_5dB;
                            val -= 5;
                        }
                    }
                }
            }
        }
        else if(val/40)
        {
            new_io &= V3_4_0dB;
            val -= 40;
            if(val/20)
            {
                new_io &= V4_2_0dB;
                val -= 20;
                if(val/10)
                {
                    new_io &= V5_1_0dB;
                    val -= 10;
                    if(val/5)
                    {
                        new_io &= V6_0_5dB;
                        val -= 5;
                    }
                }
            }
        }
        else if(val/20)
        {
            new_io &= V4_2_0dB;
            val -= 20;
            if(val/10)
            {
                new_io &= V5_1_0dB;
                val -= 10;
                if(val/5)
                {
                    new_io &= V6_0_5dB;
                    val -= 5;
                }
            }
        }
        else if(val/10)
        {
            new_io &= V5_1_0dB;
            val -= 10;
            if(val/5)
            {
                new_io &= V6_0_5dB;
                val -= 5;
            }
        }
        else if(val/5)
        {
            new_io &= V6_0_5dB;
        }
        //将新的GPIO值左移10位，再或上之前的GPIO值，就可以写入GPIOB了
        new_io <<= 10;
        GPIO_Write(GPIOB ,new_io|now_io);
        return new_io;
    }
}
